package github.kawaiior.yuanchuxiuzhen.common.tileentity;

import github.kawaiior.yuanchuxiuzhen.Yuanchuxiuzhen;
import github.kawaiior.yuanchuxiuzhen.common.container.PelletFurnaceContainer;
import github.kawaiior.yuanchuxiuzhen.common.container.number.PelletFurnaceContainerItemNumber;
import github.kawaiior.yuanchuxiuzhen.common.item.fire.StrangeFireItem;
import github.kawaiior.yuanchuxiuzhen.common.item.misc.HerbItem;
import github.kawaiior.yuanchuxiuzhen.common.item.misc.PelletItem;
import github.kawaiior.yuanchuxiuzhen.common.item.misc.RefinePelletItem;
import github.kawaiior.yuanchuxiuzhen.core.init.ItemInit;
import github.kawaiior.yuanchuxiuzhen.core.init.TileEntityInit;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;

import javax.annotation.Nullable;

public class PelletFurnaceTileEntity extends TileEntity implements ITickableTileEntity, INamedContainerProvider {

    private PelletFurnaceContainerItemNumber itemNumber = new PelletFurnaceContainerItemNumber();

    private final String herbInventoryString = "herb_inventory_";
    private final String pelletInventoryString = "pellet_inventory";
    private final String fireInventoryString = "fire_inventory";
    private final String refinePelletInventoryString = "refine_pellet_inventory";

    private Inventory herbInventory = new Inventory(4);
    private Inventory pelletInventory = new Inventory(1);
    private Inventory fireInventory = new Inventory(1);
    private Inventory refinePelletInventory = new Inventory(1);

    private boolean working = false;

    private int tickTime = 0;

    public PelletFurnaceTileEntity(TileEntityType<?> tileEntityTypeIn) {
        super(tileEntityTypeIn);
    }
    public PelletFurnaceTileEntity() {
        super(TileEntityInit.PELLET_FURNACE_TILE_ENTITY.get());
    }
    @Override
    public ITextComponent getDisplayName() {
        return new TranslationTextComponent("container."+ Yuanchuxiuzhen.MODID+".example_gui_block");
    }

    @Nullable
    @Override
    public Container createMenu(int id, PlayerInventory playerInventory, PlayerEntity player) {
        return new PelletFurnaceContainer(id, playerInventory, this, itemNumber);
    }

    @Override
    public void tick() {
        tickTime++;
        if (tickTime<20){
            return;
        }
        tickTime = 0;

        if (!working){
            return;
        }
        if (itemNumber.get(0) == itemNumber.getMax()){
            itemNumber.reset();
            working = false;
            // 生成产物
            genPellet();
            // 清空材料
            for (int i = 0; i < 4; i++) {
                herbInventory.setInventorySlotContents(i, ItemStack.EMPTY);
            }
            pelletInventory.setInventorySlotContents(0, ItemStack.EMPTY);
            fireInventory.setInventorySlotContents(0, ItemStack.EMPTY);
        }

        Yuanchuxiuzhen.debug("PelletFurnace working tick is "+itemNumber.get(0));
        itemNumber.tick();
    }

    @Override
    public void read(BlockState state, CompoundNBT nbt) {
        for (int i = 0; i < 4; i++) {
            this.herbInventory.setInventorySlotContents(i, ItemStack.read(nbt.getCompound(herbInventoryString+i)));
        }
        this.pelletInventory.setInventorySlotContents(0, ItemStack.read(nbt.getCompound(pelletInventoryString)));
        this.fireInventory.setInventorySlotContents(0, ItemStack.read(nbt.getCompound(fireInventoryString)));
        this.refinePelletInventory.setInventorySlotContents(0, ItemStack.read(nbt.getCompound(refinePelletInventoryString)));

        super.read(state, nbt);
    }

    @Override
    public CompoundNBT write(CompoundNBT compound) {
        for (int i = 0; i < 4; i++) {
            ItemStack stack = this.herbInventory.getStackInSlot(i).copy();
            compound.put(herbInventoryString+i, stack.serializeNBT());
        }
        compound.put(pelletInventoryString, this.pelletInventory.getStackInSlot(0).copy().serializeNBT());
        compound.put(fireInventoryString, this.fireInventory.getStackInSlot(0).copy().serializeNBT());
        compound.put(refinePelletInventoryString, this.refinePelletInventory.getStackInSlot(0).copy().serializeNBT());
        return super.write(compound);
    }

    private void genPellet(){
        byte[] effects = new byte[4];
        for (int i = 0; i < 4; i++) {
            ItemStack stack = herbInventory.getStackInSlot(i);
            if (stack.getItem() instanceof HerbItem){
                effects[i] = (byte) ((HerbItem)stack.getItem()).getId();
            }else {
                return;
            }
        }
        ItemStack fireStack = fireInventory.getStackInSlot(0);
        byte fireLevel = 0;
        if (fireStack.getItem() instanceof StrangeFireItem){
            fireLevel = (byte) ((StrangeFireItem)fireStack.getItem()).getLevel();
        }else {
            return;
        }
        ItemStack pelletStack = pelletInventory.getStackInSlot(0);
        if (pelletStack.getItem() instanceof PelletItem){
            ItemStack refinePelletStack = RefinePelletItem.getBaseRefinePellet(((PelletItem)pelletStack.getItem()).getManaRoot());
            RefinePelletItem.initRefinePellet(refinePelletStack, fireLevel, effects);
            if (refinePelletInventory.isEmpty()){
                refinePelletInventory.setInventorySlotContents(0, refinePelletStack);
            }
        }
    }

    public Inventory getHerbInventory() {
        return herbInventory;
    }

    public Inventory getPelletInventory() {
        return pelletInventory;
    }

    public Inventory getFireInventory() {
        return fireInventory;
    }

    public Inventory getRefinePelletInventory() {
        return refinePelletInventory;
    }

    public PelletFurnaceContainerItemNumber getItemNumber() {
        return itemNumber;
    }

    public void checkWorking(){
        for (int i = 0; i < 4; i++) {
            if (herbInventory.getStackInSlot(i).isEmpty()){
                return;
            }
        }
        if (pelletInventory.getStackInSlot(0).isEmpty()){
            return;
        }
        if (fireInventory.getStackInSlot(0).isEmpty()){
            return;
        }
        if (refinePelletInventory.getStackInSlot(0).isEmpty()){
            this.working = true;
            Yuanchuxiuzhen.debug("PelletFurnace is working !");
        }
    }

    public boolean isWorking() {
        return working;
    }

    public void setWorking(boolean working) {
        this.working = working;
    }
}
